<?php

namespace Examine\Controller;

use Common\Controller\BaseController;
use Think\Page;

class IndexController extends BaseController
{
    public function index()
    {
        $task_type = 2;
        $model = D('VUserTask');
        $sp = $_GET['p'];
        if (empty($sp)) {
            $sp = 1;
        }
        $search_key = $_POST['search_key'];
        $status = $_POST['search_status'];

        $this->assign('task_type', $task_type);

        $user_id = $this->current_user['id'];
        $list = $model->get_list($user_id, $search_key, $status, $task_type, null, $sp, C('PAGE_SIZE'));

        $page = new Page($list[1], C('PAGE_SIZE'));

        $this->assign('total_rows', $page->totalRows);
        $this->assign('_page', $page);
        $this->assign('page_link', $page->show());
        $this->assign('task_list', $list[0]);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }
        $this->display();
    }

    public function detail()
    {
        if (empty($taskid)) {
            $taskid = $_POST['taskid'];
        }
        $search_key = $_POST['search_key'];

        $user_id = null;
        $role = $this->current_user['role'];
        if ($role == 3) {
            $user_id = $this->current_user['id'];
        }
        $model = D('VUserTask');
        $list0 = $model->statistics('2018', 0, $user_id);
        $list1 = $model->statistics('2018', 1, $user_id);
        $list2 = $model->statistics('2018', 2, $user_id);

        $list = array();
        for ($i = 0; $i < count($list0); $i++) {
            $item0 = $list0[$i];
            $item1 = $list1[$i];
            $item2 = $list2[$i];
            $s0 = $this->get_score($item0);
            $s1 = $this->get_score($item1);
            $s2 = $this->get_score($item2);
            $row = array('user_id' => $item0['user_id'],
                'task0' => $s0,
                'task1' => $s1,
                'task2' => $s2);
            $list[] = $row;
        }
//        $page = new Page($list[1], C('PAGE_SIZE'));
        $this->assign('user_list', $list);
//        $this->assign('_page', $page);

        foreach ($_POST as $key => $val) {
            $page->parameter[$key] = urlencode($val);
        }
        $this->display();
    }

    private function get_score($item)
    {
        $r = $item['amount_score'];
        if (empty($r)) {
            return 0;
        }
        return $r;
    }
}
